home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 May / macformat-024.iso / Shareware City / Developers / nshellmegasource1.50 / mega src / commands / unlock.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  6.1 KB  |  298 lines  |  [TEXT/KAHL]

  1. /* ========== the commmand file: ==========
  2.  
  3.     unlock.c
  4.     
  5.     Copyright (c) 1994 Newport Software Development
  6.     
  7.     You may distribute unmodified copies of this file for
  8.     noncommercial purposes.  You may use this file as a
  9.     reference when writing your own nShell(tm) commands.
  10.     
  11.     All other rights are reserved.
  12.     
  13.    ========== the commmand file: ========== */
  14.  
  15. //
  16. // This code can build to be "lock" or "unlock".
  17. //
  18. // To build "lock", define LOCK below.
  19. //
  20. // To build "unlock", define UNLOCK below.
  21. //
  22. // But only define one!
  23. //
  24.  
  25. #define  UNLOCK
  26.  
  27. #ifdef __MWERKS__            // CodeWarrior requires an A4 setup
  28. #include <A4Stuff.h>
  29. #endif
  30.  
  31. #include <GestaltEqu.h>
  32.  
  33. #include "nshc.h"
  34.  
  35. #include "arg_utl.proto.h"
  36. #include "nshc_utl.proto.h"
  37. #include "fss_utl.proto.h"
  38. #include "str_utl.proto.h"
  39.  
  40. // data definition - this struct is the root of all data
  41.  
  42. typedef struct {
  43.  
  44.     int        is_seven;        // 0 if this is a pre-System 7 environment
  45.     int        arg;            // position in arg list
  46.  
  47. } t_unlock_data;
  48.  
  49. typedef    t_unlock_data    **unlock_hndl;
  50.  
  51. /* ======================================== */
  52.  
  53. // prototypes - utility
  54.  
  55. int  unlock_is_seven( void );
  56. void unlock_bad( t_nshc_parms *nshc_parms, int code );
  57. void unlock_bad_file( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, StringPtr msg );
  58. void unlock_good( t_nshc_parms *nshc_parms );
  59.  
  60. // prototypes - file routines
  61.  
  62. void  unlock_one( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, t_unlock_data **hData );
  63.  
  64. // prototypes - state machine
  65.  
  66. void unlock_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls  );
  67. void unlock_continue( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  68. void unlock_stop( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls );
  69.  
  70. /* ======================================== */
  71.  
  72. // return true when OS is system 7 or later
  73.  
  74. int    unlock_is_seven( void )
  75. {
  76.     long    response;
  77.     
  78.     if ( Gestalt( 'sysv', &response ) )
  79.         return( 1 );
  80.         
  81.     if ( response < 0x700 )
  82.         return( 1 );
  83.     else
  84.         return( 0 );
  85. }
  86.  
  87. /* ======================================== */
  88.  
  89. void unlock_bad(  t_nshc_parms *nshc_parms, int code )
  90. {
  91.     nshc_parms->action = nsh_stop;
  92.     nshc_parms->result = code;
  93. }
  94.     
  95. /* ======================================== */
  96.  
  97. void unlock_bad_file(  t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, StringPtr msg )
  98. {
  99.  
  100.     nshc_calls->NSH_putStr_err("\p(un)lock: File access error (");
  101.     nshc_calls->NSH_putStr_err(msg);
  102.     nshc_calls->NSH_putStr_err("\p)\r");
  103.  
  104.     nshc_parms->action = nsh_stop;
  105.     nshc_parms->result = NSHC_ERR_GENERAL;
  106. }
  107.     
  108. /* ======================================== */
  109.  
  110. void unlock_good(  t_nshc_parms *nshc_parms )
  111. {
  112.     nshc_parms->action = nsh_stop;
  113.     nshc_parms->result = 0;
  114. }
  115.  
  116. /* ======================================== */
  117.  
  118. // file access routines
  119.  
  120. /* ======================================== */
  121.  
  122. void unlock_one( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls, t_unlock_data **hData )
  123. {
  124.     int            result;
  125.     int            dont_worry;
  126.     Str255        pathname;
  127.     FSSpec        fss;
  128.     
  129.     // =====> convert argument to pathname
  130.     
  131.     result = arg_to_str( nshc_parms, nshc_calls, (**hData).arg++, pathname );
  132.  
  133.     if (result) {
  134.         unlock_bad( nshc_parms, result );
  135.         return;
  136.         }
  137.         
  138.     result = nshc_calls->NSH_path_expand( pathname );
  139.  
  140.     if (result) {
  141.         unlock_bad_file(  nshc_parms, nshc_calls, pathname );
  142.         return;
  143.         }
  144.  
  145.     // =====> lock or unlock it
  146.  
  147.     #ifdef LOCK
  148.  
  149.         result = SetFLock( pathname, 0 );
  150.  
  151.     #else
  152.  
  153.         result = RstFLock( pathname, 0 );
  154.  
  155.     #endif
  156.  
  157.     if (!result) {        // try to wake up the parent - but don't worry
  158.  
  159.         dont_worry = nshc_calls->NSH_path_expand( pathname );
  160.  
  161.         if (!dont_worry)
  162.             dont_worry = nshc_calls->NSH_path_to_FSSpec( pathname, &fss );
  163.  
  164.         if (!dont_worry)
  165.             dont_worry = fss_wake_parent( &fss );
  166.  
  167.         }
  168.  
  169.     if (result)
  170.         unlock_bad_file(  nshc_parms, nshc_calls, pathname );
  171. }
  172.  
  173. /* ======================================== */
  174.  
  175. // state machine - core routines
  176.  
  177. /* ======================================== */
  178.  
  179. void unlock_start( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls  )
  180. {
  181.     unlock_hndl    hData;    // handle to hold our data
  182.     int            one;
  183.     
  184.     // check to see that the developer defined LOCK or UNLOCK, but not both
  185.     
  186.     one = 0;
  187.     
  188.     #ifdef LOCK
  189.         one++;
  190.     #endif
  191.     
  192.     #ifdef UNLOCK
  193.         one++;
  194.     #endif
  195.     
  196.     if ( one != 1 ) {
  197.         nshc_calls->NSH_putStr_err( "\p(un)lock: Your programmer goofed on LOCK or UNLOCK.\r" );
  198.         unlock_bad( nshc_parms, NSHC_ERR_GENERAL );
  199.         return;
  200.         }
  201.         
  202.     // check usage
  203.  
  204.     if (nshc_parms->argc < 2) {
  205.     
  206.         #ifdef LOCK
  207.         
  208.             nshc_calls->NSH_putStr_err( "\pUsage: lock file [file...]\r" );
  209.             
  210.         #else
  211.         
  212.             nshc_calls->NSH_putStr_err( "\pUsage: unlock file [file...]\r" );
  213.             
  214.         #endif
  215.         
  216.         unlock_bad( nshc_parms, NSHC_ERR_PARMS );
  217.         return;
  218.         }
  219.     
  220.     // ok, set up data
  221.     
  222.     nshc_parms->action = nsh_continue;
  223.  
  224.     hData = (unlock_hndl)NewHandleClear(sizeof(t_unlock_data));
  225.     
  226.     if (hData) {
  227.     
  228.         (**hData).arg = 1;                        // start at the arg = 1 position
  229.         (**hData).is_seven = unlock_is_seven();    // test os revision
  230.         nshc_parms->data = (Handle)hData;        // and file it
  231.         }
  232.     else {
  233.         nshc_calls->NSH_putStr_err( "\p(un)lock: Could not allocate storage.\r" );
  234.         unlock_bad( nshc_parms, NSHC_ERR_MEMORY );
  235.         }
  236. }
  237.  
  238. /* ======================================== */
  239.  
  240. void unlock_continue( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  241. {
  242.     int            i;
  243.     int            arg;
  244.     unlock_hndl    hData;
  245.     
  246.     if (hData = (unlock_hndl)nshc_parms->data) {
  247.     
  248.         arg = (**hData).arg;
  249.         
  250.         if (arg >= nshc_parms->argc)
  251.             unlock_good( nshc_parms );
  252.         else
  253.             unlock_one( nshc_parms, nshc_calls, hData );
  254.  
  255.         }
  256. }
  257.  
  258. /* ======================================== */
  259.  
  260. void unlock_stop( t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls )
  261. {
  262.     if (nshc_parms->data)
  263.         DisposeHandle(nshc_parms->data);
  264.         
  265.     nshc_parms->action = nsh_idle;
  266. }
  267.  
  268. /* ======================================== */
  269.  
  270. void main(t_nshc_parms *nshc_parms, t_nshc_calls *nshc_calls)
  271. {    
  272. #ifdef __MWERKS__
  273.     long oldA4  = SetCurrentA4();
  274. #endif
  275.  
  276.     if ( !nshc_bad_version( nshc_parms, nshc_calls, NSHC_VERSION ) ) {
  277.     
  278.         switch (nshc_parms->action) {
  279.             case nsh_start:
  280.                 unlock_start(nshc_parms, nshc_calls);
  281.                 break;
  282.             case nsh_continue:
  283.                 unlock_continue(nshc_parms, nshc_calls);
  284.                 break;
  285.             case nsh_stop:
  286.                 unlock_stop(nshc_parms, nshc_calls);
  287.                 break;
  288.             }
  289.         
  290.         }
  291.     
  292. #ifdef __MWERKS__
  293.     SetA4(oldA4);
  294. #endif
  295. }
  296.  
  297. /* ======================================== */
  298.